package com.yzs.common.config;

import com.yzs.Interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Autowired
    MyInterceptor myInterceptor;

    private static final List<String> EXCLUDE_PATH = Arrays.asList("/", "css/**", "js/**", "img/**", "json/**", "fonts/**","/*.html",
            "/houseImg/**",
            "/admin/login","/admin/getHouseAdminById",
            "/house/getNewHouse","/house/getHouseByPage","/house/getHouseById","/house/getHouseByPageAsFilter","/house/getVillage","/house/searchHouseByVillage",
            "/user/getSalerById","/user/loginUser","/user/registerUser","/user/getVerifyCode","/user/forgetPwd",
            "/data/**");
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns(EXCLUDE_PATH);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        设置允许跨域的路径
        registry.addMapping("/**")
//                设置允许跨域请求的域名
                .allowedOriginPatterns("*")
//                是否允许证书
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT","OPTIONS")
                .allowedHeaders("*")
//                跨域允许时间
                .maxAge(3600);
    }

}
